<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('after_sales_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('request_id')->constrained('after_sales_requests')->onDelete('cascade')->comment('售后申请ID');
            $table->foreignId('order_item_id')->constrained('order_items')->onDelete('cascade')->comment('订单项ID');
            
            // 商品信息
            $table->integer('quantity')->comment('申请数量');
            
            // 处理信息
            $table->decimal('refund_amount', 10, 2)->default(0)->comment('退款金额');
            $table->text('admin_comment')->nullable()->comment('管理员备注');
            
            // 审计字段
            $table->decimal('original_price', 10, 2)->nullable()->comment('商品原价');
            $table->text('calculation_note')->nullable()->comment('计算说明');
            $table->timestamp('calculated_at')->nullable()->comment('计算时间');
            
            $table->timestamps();
            
            // 索引
            $table->index(['request_id', 'order_item_id']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('after_sales_items');
    }
};
